package org.lvgo.jx3.furniture.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.lvgo.jx3.furniture.entity.FurnitureInfo;
import org.lvgo.jx3.furniture.mapper.FurnitureInfoMapper;
import org.lvgo.jx3.furniture.service.IFurnitureInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 家具基础表 服务实现类
 * </p>
 *
 * @author lvgorice@gmail.com
 * @since 2020-06-20
 */
@Service
public class FurnitureInfoServiceImpl extends ServiceImpl<FurnitureInfoMapper, FurnitureInfo> implements IFurnitureInfoService {

    @Resource
    private FurnitureInfoMapper furnitureInfoMapper;

    /**
     * 更具类别获取家具列表
     *
     * @param type    家具type值
     * @param current 页码
     * @param size    每页大小
     * @return 分页家具
     */
    @Override
    public Page<FurnitureInfo> getFurnitureListByCategory(Integer type, int current, int size, String name, Integer level, int order, Integer source) {
        LambdaQueryWrapper<FurnitureInfo> query = Wrappers.lambdaQuery(FurnitureInfo.class);
        query.eq(FurnitureInfo::getType, type);
        return this.page(queryParam(current, size, name, level, order, source, query), query);
    }

    /**
     * 获取全部家具列表
     *
     * @param current 页码
     * @param size    每页大小
     * @return 分页家具
     */
    @Override
    public Page<FurnitureInfo> getFurnitureList(int current, int size, String name, Integer level, int order, Integer source) {
        LambdaQueryWrapper<FurnitureInfo> query = Wrappers.lambdaQuery(FurnitureInfo.class);
        return this.page(queryParam(current, size, name, level, order, source, query), query);
    }

    /**
     * 获取所有家具类别
     * SELECT
     * 	type,
     * 	type_desc
     * FROM
     * 	furniture_info
     * GROUP BY
     * 	type,
     * 	type_desc
     * @return 家具类别 Map
     */
    @Override
    public List<Map<String, Object>> getFurnitureCategories() {
//        return this.listMaps(Wrappers.lambdaQuery(FurnitureInfo.class).groupBy(FurnitureInfo::getType).groupBy(FurnitureInfo::getTypeDesc));
        return furnitureInfoMapper.getFurnitureCategories();
    }


    /**
     * 获取家具副本类别
     *
     * @return 家具副本
     */
    @Override
    public List<Map<String, Object>> getFurnitureSourceOptions() {
        return furnitureInfoMapper.getFurnitureSourceOptions();
    }

    /**
     * 获取家具来源列表
     *
     * @return 家具来源列表
     */
    @Override
    public List<Map<String, Object>> getFurnitureSources() {
        return furnitureInfoMapper.getFurnitureSources();
    }

    private Page<FurnitureInfo> queryParam(int current, int size, String name, Integer level, int order, Integer source, LambdaQueryWrapper<FurnitureInfo> query) {
        Page<FurnitureInfo> page = new Page<>(current, size);
        // 根据家具名称模糊查询
        if (StringUtils.isNotBlank(name)) {
            query.like(FurnitureInfo::getName, name);
        }
        // 根据家具品质
        if (level != null) {
            query.eq(FurnitureInfo::getLevel, level);
        }
        // 根据家具来源
        if (source != null) {
            query.eq(FurnitureInfo::getSource, source);
        }
        // 排序规则
        /*
         * 0 默认
         * 1 观赏
         * 2 实用
         * 3 坚固
         * 4 风水
         * 5 趣味
         */
        switch (order) {
            case 1:
                query.orderByDesc(FurnitureInfo::getGuanShang);
                break;
            case 2:
                query.orderByDesc(FurnitureInfo::getShiYong);
                break;
            case 3:
                query.orderByDesc(FurnitureInfo::getJianGu);
                break;
            case 4:
                query.orderByDesc(FurnitureInfo::getFengShui);
                break;
            case 5:
                query.orderByDesc(FurnitureInfo::getQuWei);
                break;
            default:
                break;
        }
        return page;
    }

}
